package com.imagine.controller.client;

import com.imagine.common.Global;
import com.imagine.entity.*;
import com.imagine.service.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.*;

import javax.jms.Queue;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by liangyue on 2017/9/26.
 */
@RestController
@EnableAutoConfiguration
@RequestMapping("/delegation")
public class DelegationController {
    @Autowired
    private DelegationService delegationService;

    @Autowired
    JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    Queue queue2;

    @ApiOperation(value="获得委托信息", notes="获得委托信息")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "int",paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String getDelegationById(@PathVariable int id) throws IOException {
        Delegation delegation = delegationService.getDelegationById(id);
        return delegation.toString();
    }



//    @ResponseBody
//    @ApiOperation(value="创建新的委托订单", notes="创建新的委托订单")
//    @RequestMapping(value = "", method = RequestMethod.POST)
//    public HashMap insertDelegation(
//            @ApiParam("证券代码") @RequestParam(value = "securityId") String securityId,
//            @ApiParam("证券名称") @RequestParam(value = "securityName") String securityName,
//            @ApiParam("操作方向") @RequestParam(value = "delegationDirection") String delegationDirection,
//            @ApiParam("申请数量") @RequestParam(value = "delegationNum") int delegationNum,
//            @ApiParam("申请价格") @RequestParam(value = "delegationPrice") double delegationPrice,
//            @ApiParam("市场均价") @RequestParam(value = "evenPrice") double evenPrice,
//            @ApiParam("交易市场") @RequestParam(value = "tradingMarket") String tradingMarket,
//            @ApiParam("货币种类") @RequestParam(value = "currency") String currency,
//            HttpSession session
//    ) throws IOException {
//        CapitalAccount  account = (CapitalAccount) session.getAttribute("account");
//        Delegation newDelegation = new Delegation(account.getcAccountId(), securityId, securityName, delegationDirection,
//                delegationNum, delegationPrice, 0, evenPrice, tradingMarket, currency, null);
//        return this.jmsMessagingTemplate.convertSendAndReceive(queue2,newDelegation, HashMap.class);
//    }

    @ResponseBody
    @ApiOperation(value="创建新的委托订单", notes="创建新的委托订单")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public HashMap insertDelegation(
            @ApiParam("账户ID") @RequestParam(value = "cAaccountId") String accountId,
            @ApiParam("证券代码") @RequestParam(value = "securityId") String securityId,
            @ApiParam("证券名称") @RequestParam(value = "securityName") String securityName,
            @ApiParam("操作方向") @RequestParam(value = "delegationDirection") String delegationDirection,
            @ApiParam("申请数量") @RequestParam(value = "delegationNum") int delegationNum,
            @ApiParam("申请价格") @RequestParam(value = "delegationPrice") double delegationPrice,
            @ApiParam("市场均价") @RequestParam(value = "evenPrice") double evenPrice,
            @ApiParam("交易市场") @RequestParam(value = "tradingMarket") String tradingMarket,
            @ApiParam("货币种类") @RequestParam(value = "currency") String currency,
            HttpSession session
    ) throws IOException {
        Delegation newDelegation = new Delegation(accountId, securityId, securityName, delegationDirection,
                delegationNum, delegationPrice, 0, evenPrice, tradingMarket, currency, null);
        return this.jmsMessagingTemplate.convertSendAndReceive(queue2,newDelegation, HashMap.class);
    }


    @ResponseBody
    @ApiOperation(value="撤销委托订单", notes="撤销委托订单")
    @RequestMapping(value = "/backout", method = RequestMethod.POST)
    public BaseResult<Object> backoutDelegation(
            @ApiParam("委托ID") @RequestParam(value = "delegationId") int delegationId
    ) throws IOException {
        return delegationService.backoutDelegation(delegationId);
    }

    @ResponseBody
    @ApiOperation(value="根据用户资金账户ID获得所有成交订单", notes="根据用户资金账户ID获得所有成交订单")
    @RequestMapping(value = "/bargain", method = RequestMethod.POST)
    public BaseResult<Object> getAllBargainByCId(HttpSession session) throws IOException {
        CapitalAccount  account = (CapitalAccount) session.getAttribute("account");
        return delegationService.getBargainByCId(account.getcAccountId());
    }

    @ResponseBody
    @ApiOperation(value="根据用户资金账户ID获得所有委托订单", notes="根据用户资金账户ID获得所有委托订单")
    @RequestMapping(value = "/all", method = RequestMethod.POST)
    public BaseResult<Object> getDelegationsByCId(HttpSession session) throws IOException {
        CapitalAccount  account = (CapitalAccount) session.getAttribute("account");
        return delegationService.getDelegationsByCId(account.getcAccountId());
    }

}
